home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / web / spiderweb / tools / tie / io.web < prev    next >
Text File  |  1992-07-05  |  3KB  |  109 lines

  1. @* Input and output.
  2.  
  3. \noindent Terminal output is done by writing on file
  4. |term_out|, which is assumed to consist of characters of
  5. type |text_char|.  Terminal input is read from |term_in|.
  6. @^system dependencies@>
  7.  
  8. @d print(X) = fprintf(stderr,X) /* `|print|' means write on the terminal */
  9. @d new_line = putc('\n',stderr) /* start new line */
  10. @d print_ln(X) = {print(X); new_line;}
  11. @d print_nl(X) = { new_line; print(X);}
  12.  
  13. @d term_in = stdin
  14. @d term_out = stderr
  15.  
  16. @ The |update_terminal| procedure is called when we want to
  17. make sure that everything we have output to the terminal so
  18. far has actually left the computer's internal buffers and
  19. been sent.
  20. @^system dependencies@>
  21.  
  22. @d update_terminal = fflush(term_out) {empty the terminal output buffer}
  23.  
  24.  
  25. @ If an error occurs we indicate this calling a procedure
  26. named |err_print|.  This procedure is implemented as a
  27. macro.    It gives a message and an indication of the
  28. offending file.  The actions to determine the error location
  29. will be explained later.
  30.  
  31. @d err_print(MSG,LOC) = {print_nl(MSG); err_loc(LOC); history=troublesome;}
  32.  
  33.  
  34. @ The basic procedure |get_ln_from_file| can be used to get
  35. a line from an input file.  The line is stored in
  36. |input_files[i].buffer|.  The components |limit| and |lineno|
  37. are updated.  If the end of the file is reached
  38. |input_files[i].mode| is set to |ignore|.  On some systems
  39. it might be useful to replace tab characters by a proper
  40. number of spaces since several editors used to create change
  41. files insert tab characters into a source file not under
  42. control of the user.  So it might be a problem to create a
  43. matching change file.
  44. @^tab character expansion@>
  45.  
  46. We define |get_line| to read a line from a file specified by
  47. number.  To ease an inplementation without arrays of files
  48. we introduce one more parameter.  In such cases |get_line|
  49. is best replaced by a procedure containing a |case|
  50. statement to select the file needed.
  51.  
  52. @d get_line(X) = get_ln_from_file(X,input_files[X]);
  53.  
  54. @c
  55.  get_ln_from_file(i,cur_file)
  56.      int i;
  57.     text_file cur_file;
  58. {
  59. int final_limit;
  60. int c;
  61.       if (input_organization[i].mode==ignore) return;
  62.       if (feof(cur_file)) @<Handle end of file and return@>;
  63.       @<Get line into buffer@>;
  64. }
  65.  
  66. @ End of file is special if this file is the master file.
  67. @<Handle end of file ...@>=
  68. {
  69.   input_organization[i].mode=ignore;
  70.   if (input_organization[i].type_of_file==master) input_has_ended=true;
  71.   return;
  72. }
  73.  
  74.  
  75. @ Lines must fit into the buffer completely.
  76. Tab character expansion might be done here.
  77. @^tab character expansion@>
  78.  
  79. @<Get line into buffer@>=
  80. (input_organization[i].lineno)++;
  81. input_organization[i].limit=0; final_limit = 0;
  82. while ((input_organization[i].limit<=buf_size) && (c=getc(cur_file)) != EOF 
  83.       && c!='\n')
  84. {
  85.   input_organization[i].buffer[input_organization[i].limit]=xord[c]; 
  86.   (input_organization[i].limit)++;
  87.   if ((input_organization[i].buffer[input_organization[i].limit-1]!=' ') 
  88.     && (input_organization[i].buffer[input_organization[i].limit-1]
  89.                 != tab_mark))
  90.       final_limit=input_organization[i].limit;
  91. }
  92.   if (input_organization[i].limit>buf_size)
  93.     if ((c=getc(cur_file))!=EOF && c!='\n') {
  94.       ungetc(c,cur_file); err_print("! Input line too long",i);
  95. @.Input line too long@>
  96.   }
  97. @<Check for end of file@>@;
  98. input_organization[i].limit=final_limit;
  99.  
  100. @ We have to check for end of file in the middle of reading
  101. @<Check for end of file@>=
  102. if (input_organization[i].limit==0 && c==EOF)
  103.     @<Handle end of file...@>@;
  104.  
  105.  
  106.  
  107.  
  108.  
  109.